package com.zuowei.ycy.user.ui.mine;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.and.yzy.frame.adapter.AdapterCallback;
import com.and.yzy.frame.util.RetrofitUtils;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.adapter.CollectAdapter;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.domain.CollectGood;
import com.zuowei.ycy.user.http.Collection;
import com.zuowei.ycy.ui.BaseToolbarAty;
import com.zuowei.ycy.user.util.AppJsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.loadmore.LoadMoreListViewContainer;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/4/27.
 */
public class MineCollectAty extends BaseToolbarAty implements AdapterCallback {

    @Bind(R.id.listview)
    ListView listview;
    @Bind(R.id.load_more_list_view_container)
    LoadMoreListViewContainer loadMoreListViewContainer;
    @Bind(R.id.ll_empty)
    LinearLayout llEmpty;
    @Bind(R.id.ptr_frame)
    PtrFrameLayout ptrFrame;

    private List<CollectGood> list;
    private CollectAdapter adapter;
    private CollectGood item;

    @Override
    public int getLayoutId() {
        return R.layout.mine_collect_aty;
    }

    @Override
    public void initData() {
        mToolbar.setTitle("我的收藏");

//        PtrInitHelper.initPtr(this,ptrFrame);
        ptrFrame.setPullToRefresh(false);
        loadMoreListViewContainer.useDefaultFooter();
        loadMoreListViewContainer.setAutoLoadMore(false);
        loadMoreListViewContainer.loadMoreFinish(false, false);
//        loadMoreListViewContainer.setLoadMoreHandler(this);

        list = new ArrayList<>();
        adapter = new CollectAdapter(this, list, R.layout.mine_colloect_item, this);
        listview.setAdapter(adapter);
        listview.setEmptyView(llEmpty);
        isShowToast =false;
    }

    @Override
    public boolean setIsInitRequestData() {
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        doHttp(RetrofitUtils.createApi(Collection.class).collectionList(UserManger.getM_id()), 0);
    }

    @Override
    public void requestData() {
        showLoadingContentDialog();
    }

    @Override
    public void adapterstartActivity(Class<?> className, Bundle options) {

    }

    @Override
    public void adapterstartActivityForResult(Class<?> className, Bundle options, int requestcode) {

    }

    @Override
    public void adapterInfotoActiity(Object data, int what) {
        item = (CollectGood) data;
        showLoadingDialog("");
        doHttp(RetrofitUtils.createApi(Collection.class).deleteCollection(UserManger.getM_id(), item.getMerchant_id()), 1);
    }


    @Override
    public void onError(Call<ResponseBody> call, Throwable t, int what) {
        if (what==0){
            isShowToast = false;
        }
        super.onError(call, t, what);
        isShowToast = true;
    }

    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {
        if (what == 0) {
            list.clear();
            list.addAll(AppJsonUtil.getArrayList(result, CollectGood.class));
            adapter.notifyDataSetChanged();
        } else if (what == 1) {
            showToast(AppJsonUtil.getResultInfo(result).getMessage());
            list.remove(item);
            adapter.notifyDataSetChanged();
        }
        super.onSuccess(result, call, response, what);
    }
}
